<?php
declare(strict_types=1);

namespace Modules\Inventory\Http\Controllers;

use Catch\Base\CatchController as Controller;
use App\Services\UnitService;use Modules\Base\Repository\MaterialRepository;use Modules\Inventory\Models\InventoryTrans;
use Illuminate\Http\Request;
use App\Enums\InvTransDocType;
use App\Enums\InvTransType;
use App\Enums\Org;


class InventoryTransController extends Controller
{
    public function __construct(
        protected readonly InventoryTrans $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {
       $list = $this->model->getList()->toArray();
       foreach ($list['data'] as &$val){
           $val['org_name']         = Org::labelBy($val['org_code']);
           $val['cargo_name']       = Org::labelBy($val['cargo_code']);
           $val['doc_type']         = InvTransDocType::labelBy($val['doc_type']);
           $val['trans_type']       = InvTransType::labelBy($val['trans_type']);
           $val['material']         = MaterialRepository::findMaterial($val['material_no']);
           $val['from_locations']   = UnitService::getLocationByCode($val['from_location']);
           $val['from_wmss']        = UnitService::getWmsByCode($val['from_wms']);
           $val['to_locations']     = UnitService::getLocationByCode($val['to_location']);
           $val['to_wmss']          = UnitService::getWmsByCode($val['to_wms']);
       }
       return $list;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        return $this->model->storeBy($request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }
}
